package com.skypan.booklistmainactivity.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.skypan.booklistmainactivity.R;
import com.skypan.booklistmainactivity.data.Hamster;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder surfaceHolder;
    private List<Hamster> hamsters;
    private Random random;


    public GameView(Context context) {
        super(context);
        initView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private float initX = 200;
    private float initY = 650;
    private float xLen = 250;
    private float yLen = 360;
    private Bitmap mouseBp, backgroundBp;

    private void initView(){
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        random = new Random();
        mouseBp = BitmapFactory.decodeResource(getResources(), R.drawable.mouse);
        backgroundBp = BitmapFactory.decodeResource(getResources(),R.drawable.background);

        mouseBp = alterBitmapSize(mouseBp, 200,200);

        hamsters = new ArrayList<>();
        for (int i = 0; i < 3; i++ ){
            for (int j = 0; j < 3; j++){
                Hamster hamster = new Hamster(initX + i * xLen, initY + j * yLen, mouseBp);
                hamsters.add(hamster);
            }
        }

    }

    private Bitmap alterBitmapSize(Bitmap bitmap, int newWidth, int newHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    }

    private DrawThread drawThread;
    private boolean isTouched = false;
    private float touchedX = -1;
    private float touchedY = -1;
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        drawThread = new DrawThread();
        drawThread.start();
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()){
                    touchedX = event.getX();
                    touchedY = event.getY();
                    isTouched = true;
                }
                return false;
            }
        });
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        drawThread.myStop();
        try {
            drawThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class DrawThread extends Thread{

        private boolean isStopped = false;

        public void myStop(){
            isStopped = true;
        }

        @Override
        public void run() {
            super.run();

            int count = 0;
            int next = -1;
            //获得画布
            Canvas canvas = null;
            while (!isStopped){
                try {
                    canvas = surfaceHolder.lockCanvas();
                    Paint paint = new Paint();
                    int Width = GameView.this.getWidth();
                    int Height = GameView.this.getHeight();

                    backgroundBp = alterBitmapSize(backgroundBp,Width,Height);
                    canvas.drawBitmap(backgroundBp,0,0,paint);


                    next = random.nextInt(9);
                    Hamster hamster = hamsters.get(next);
                    hamster.draw(canvas);


                    paint.setTextSize(100);
                    paint.setColor(Color.GREEN);
                    canvas.drawText("hit "+count,100,100,paint);
                }catch (Exception e){

                }finally {
                    if (canvas != null){
                        //释放canvas对象并提交画布
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
                try {
                    for (int i = 10; i > 0; i--){
                        Thread.sleep(100);
                        if (isTouched && hamsters.get(next).isShot(touchedX,touchedY)){
                            // 进行完处理后
                            isTouched = false;
                            count++;
                            break;
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
